package wzh000;



import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.security.KeyStore;


import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;

public class NodeCA {
	
	private static final int DEFAULT_PORT = 7777;

	private static final String KEY_STORE_PASSWORD = "123456";
	private static final String TRUST_KEY_STORE_PASSWORD = "123456";

	
	private SSLSocket socket;	
	private SSLContext ctx;
	
	
	public NodeCA() {
		try {
			this.ctx = SSLContext.getInstance("SSL");

			KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
			TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");

			KeyStore ks = KeyStore.getInstance("JKS");
			KeyStore tks = KeyStore.getInstance("JKS");

			ks.load(new FileInputStream("cacert_keystore.jks"),
					KEY_STORE_PASSWORD.toCharArray());
			tks.load(new FileInputStream("cacert_keystore.jks"),
					TRUST_KEY_STORE_PASSWORD.toCharArray());

			kmf.init(ks, KEY_STORE_PASSWORD.toCharArray());
			tmf.init(tks);

			this.ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void sendCRL(String ip, String filename){
		InputStream in = null;
		BufferedReader br = null;
		
		
		BufferedInputStream bis = null;
		
		OutputStream os = null;
		try {
			this.socket = (SSLSocket) this.ctx.getSocketFactory()
					.createSocket(ip, 42083);
		} catch (IOException e) {

			e.printStackTrace();
		}
		if (this.socket == null) {
			System.exit(1);
		}
		File myFile = new File(filename);
		byte[] mybytearray = new byte[(int) myFile.length()];
		try {
			in = this.socket.getInputStream();
			bis = new BufferedInputStream(new FileInputStream(myFile));
			bis.read(mybytearray, 0, mybytearray.length);
			os = this.socket.getOutputStream();
			os.write(mybytearray, 0, mybytearray.length);
			os.flush();
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally{
			try {
				bis.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

		try {
			
			br = new BufferedReader(new InputStreamReader(in));
			String recv;

			if ((recv = br.readLine()) != null) {
				System.out.println("After sending crl, recv: "
						+ recv);
				if (recv.equals("ACK")) {
					System.out.println(filename + " sent successfully");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (this.socket != null) {
				try {
					in.close();
					this.socket.close();
					
					
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NodeCA ca = new NodeCA();
		ca.sendCRL(args[0], args[1]);

	}

}
